//
//  HLRtcResultView.m
//  YueYue
//
//  Created by hulu001 on 2023/8/18.
//

#import "HLRtcResultView.h"
#import "HLRtcEvaluateRequest.h"
#import "HLReportRequest.h"

@interface HLRtcResultView ()

@property (nonatomic, strong) QMUIButton *reportButton;
@property (nonatomic, strong) QMUIButton *closeButton;

@property (nonatomic, strong) UIImageView *otherAvatarView;
@property (nonatomic, strong) UILabel *otherNicknameLabel;

@property (nonatomic, strong) UIImageView *meAvatarView;
@property (nonatomic, strong) UILabel *meNicknameLabel;

@property (nonatomic, strong) UILabel *rtcTimeTitleLabel;
@property (nonatomic, strong) UILabel *rtcTimeLabel;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) QMUIButton *generlButton;
@property (nonatomic, strong) QMUIButton *likeButton;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) QMUIFloatLayoutView *labelsView;
@property (nonatomic, strong) QMUIButton *submitButton;

@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) HLRtcEvaluateLabelModel *currentReason;
@property (nonatomic, strong) NSArray<HLRtcEvaluateLabelModel *>*reasonList;

@property (nonatomic, strong) HLReportModel *currentReport;
@property (nonatomic, strong) NSArray<HLReportModel *>*reportList;

@property (nonatomic, strong) HLUserInfoModel *user;
@property (nonatomic, strong) HLCommonCallbackBoolBlock closeCallback;


@end

@implementation HLRtcResultView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = UIColorWhite;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 20;
    self.reportButton = [[QMUIButton alloc] init];
    [_reportButton setTitle:@"举报" forState:UIControlStateNormal];
    [_reportButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    [_reportButton.titleLabel setFont:UIFontMake(12)];
    //[_reportButton addTarget:self action:@selector(reportAction) forControlEvents:UIControlEventTouchUpInside];
    _reportButton.hidden = YES;
    [self addSubview:_reportButton];
    [_reportButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(16);
    }];
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"rtc_result_close") forState:UIControlStateNormal];
    [self addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.size.mas_equalTo(32);
        make.centerY.equalTo(_reportButton);
    }];
    
    self.otherAvatarView = [[UIImageView alloc] init];
    [_otherAvatarView setImage:[HLUIHelper avatarBigPlaceholder]];
    _otherAvatarView.layer.masksToBounds = YES;
    _otherAvatarView.layer.cornerRadius = 32;
    [self addSubview:_otherAvatarView];
    [_otherAvatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(64);
        make.top.mas_equalTo(50);
        make.right.equalTo(self.mas_centerX).offset(-60);
    }];
    
    self.otherNicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:HLTextColor1];
    _otherNicknameLabel.text = @"萌萌仙小玩";
    [self addSubview:_otherNicknameLabel];
    [_otherNicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_otherAvatarView);
        make.top.equalTo(_otherAvatarView.mas_bottom).offset(8);
    }];
    
    self.meAvatarView = [[UIImageView alloc] init];
    [_meAvatarView setImage:[HLUIHelper avatarBigPlaceholder]];
    _meAvatarView.layer.masksToBounds = YES;
    _meAvatarView.layer.cornerRadius = 32;
    [self addSubview:_meAvatarView];
    [_meAvatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.centerY.equalTo(_otherAvatarView);
        make.left.equalTo(self.mas_centerX).offset(60);
    }];
    
    self.meNicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:HLTextColor1];
    _meNicknameLabel.text = @"我";
    [self addSubview:_meNicknameLabel];
    [_meNicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_meAvatarView);
        make.top.equalTo(_meAvatarView.mas_bottom).offset(8);
    }];
    
    
    self.rtcTimeTitleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(12) textColor:HLTextColor3];
    _rtcTimeTitleLabel.text = @"视频时长";
    [self addSubview:_rtcTimeTitleLabel];
    [_rtcTimeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(60);
    }];
    
    self.rtcTimeLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(16) textColor:HLTextColor1];
    _rtcTimeLabel.text = @"00:23:23";
    [self addSubview:_rtcTimeLabel];
    [_rtcTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_rtcTimeTitleLabel);
        make.top.equalTo(_rtcTimeTitleLabel.mas_bottom).offset(4);
    }];
    
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = HLLineColor;
    [self addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(4);
        make.top.equalTo(_otherNicknameLabel.mas_bottom).offset(24);
    }];
    
    self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(14) textColor:HLTextColor1];
    _titleLabel.text = @"您对本次约会还满意吗？";
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_lineView.mas_bottom).offset(16);
    }];
    
    self.generlButton = [[QMUIButton alloc] init];
    [_generlButton setImage:UIImageMake(@"rtc_result_generl_hl") forState:UIControlStateNormal];
    [_generlButton setImage:UIImageMake(@"rtc_result_generl") forState:UIControlStateSelected];
    [_generlButton setTitle:@"一般" forState:UIControlStateNormal];
    [_generlButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_generlButton setTitleColor:HLMainColor forState:UIControlStateSelected];
    [_generlButton.titleLabel setFont:UIFontMediumMake(14)];
    [_generlButton addTarget:self action:@selector(evaluateAction:) forControlEvents:UIControlEventTouchUpInside];
    _generlButton.imagePosition = QMUIButtonImagePositionTop;
    _generlButton.spacingBetweenImageAndTitle = 8;
    [self addSubview:_generlButton];
    [_generlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(16);
        make.right.equalTo(self.mas_centerX).offset(-54);
    }];
    
    self.likeButton = [[QMUIButton alloc] init];
    [_likeButton setImage:UIImageMake(@"rtc_result_like_hl") forState:UIControlStateNormal];
    [_likeButton setImage:UIImageMake(@"rtc_result_like") forState:UIControlStateSelected];
    [_likeButton setTitle:@"喜欢" forState:UIControlStateNormal];
    [_likeButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_likeButton setTitleColor:HLMainColor forState:UIControlStateSelected];
    [_likeButton.titleLabel setFont:UIFontMediumMake(14)];
    [_likeButton addTarget:self action:@selector(evaluateAction:) forControlEvents:UIControlEventTouchUpInside];
    _likeButton.imagePosition = QMUIButtonImagePositionTop;
    _likeButton.spacingBetweenImageAndTitle = 8;
    [self addSubview:_likeButton];
    [_likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_generlButton);
        make.left.equalTo(self.mas_centerX).offset(54);
    }];
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_generlButton.mas_bottom).offset(14);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(108);
    }];
    
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    self.labelsView = [[QMUIFloatLayoutView alloc] init];
    _labelsView.padding = UIEdgeInsetsMake(4, 4, 4, 4);
    _labelsView.itemMargins = UIEdgeInsetsMake(4, 8, 4, 8);
    _labelsView.minimumItemSize = CGSizeMake(48, 32);// 以2个字的按钮作为最小宽度
    [_contentView addSubview:_labelsView];
    [_labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(236);
    }];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_labelsView.mas_bottom).offset(-8);
    }];
    
    self.submitButton = [[QMUIButton alloc] init];
    [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [_submitButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_submitButton.titleLabel setFont:UIFontMediumMake(16)];
    [_submitButton setBackgroundColor:HLMainColor];
    [_submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    _submitButton.cornerRadius  = 12;
    [self addSubview:_submitButton];
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.scrollView.mas_bottom).offset(20);
    }];
    
    self.bottomView = [[UIView alloc] init];
    _bottomView.backgroundColor = UIColorWhite;
    [self addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
}


- (void)updateLabels
{
    [_labelsView qmui_removeAllSubviews];
    
    if (self.likeButton.isSelected){
        NSInteger tagIndex = 0;
        for (HLRtcEvaluateLabelModel *reason in self.reasonList) {
            QMUIButton *labelButton = [[QMUIButton alloc] init];
            [labelButton setTitle:reason.labelName forState:UIControlStateNormal];
            [labelButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
            [labelButton.titleLabel setFont:UIFontMake(14)];
            labelButton.layer.masksToBounds = YES;
            labelButton.layer.cornerRadius = 16;
            labelButton.backgroundColor = HLBackgroundColor;
            labelButton.contentEdgeInsets = UIEdgeInsetsMake(2, 16, 2, 16);
            labelButton.tag = tagIndex;
            [labelButton addTarget:self action:@selector(labelAction:) forControlEvents:UIControlEventTouchUpInside];
            [_labelsView addSubview:labelButton];
            
            tagIndex += 1;
        }
    } else {
        NSInteger tagIndex = 0;
        for (HLReportModel *reason in self.reportList) {
            QMUIButton *labelButton = [[QMUIButton alloc] init];
            [labelButton setTitle:reason.reportClassName forState:UIControlStateNormal];
            [labelButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
            [labelButton.titleLabel setFont:UIFontMake(14)];
            labelButton.layer.masksToBounds = YES;
            labelButton.layer.cornerRadius = 16;
            labelButton.backgroundColor = HLBackgroundColor;
            labelButton.contentEdgeInsets = UIEdgeInsetsMake(2, 16, 2, 16);
            labelButton.tag = tagIndex;
            [labelButton addTarget:self action:@selector(reportAction:) forControlEvents:UIControlEventTouchUpInside];
            [_labelsView addSubview:labelButton];
            
            tagIndex += 1;
        }
    }
}

//- (void)reportAction
//{
//    HLReportViewController *reportVC = [[HLReportViewController alloc] init];
//    [self.qmui_viewController.navigationController pushViewController:reportVC animated:YES];
//}

- (void)labelAction:(QMUIButton *)labelButton
{
    [_labelsView.subviews enumerateObjectsUsingBlock:^(__kindof QMUIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setBackgroundColor:HLBackgroundColor];
        [obj setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    }];
    [labelButton setBackgroundColor:[HLMainColor colorWithAlphaComponent:0.2]];
    [labelButton setTitleColor:HLMainColor forState:UIControlStateNormal];
    self.currentReason = self.reasonList[labelButton.tag];
}

- (void)reportAction:(QMUIButton *)labelButton
{
    [_labelsView.subviews enumerateObjectsUsingBlock:^(__kindof QMUIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setBackgroundColor:HLBackgroundColor];
        [obj setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    }];
    [labelButton setBackgroundColor:[HLMainColor colorWithAlphaComponent:0.2]];
    [labelButton setTitleColor:HLMainColor forState:UIControlStateNormal];
    self.currentReport = self.reportList[labelButton.tag];
}


- (void)setUser:(HLUserInfoModel *)user
{
    _user = user;
    
    self.likeButton.selected = YES;
    
    HLRtcEvaluateRequest *evaluateLabelRequest = [[HLRtcEvaluateRequest alloc] initWithType:HLRtcEvaluateTypeLabel params:@{}];
    [evaluateLabelRequest startWithCompletionBlockWithSuccess:^(__kindof HLRtcEvaluateRequest * _Nonnull request) {
        if (request.isSuccess){
            self.reasonList = [request labelList];
            [self updateLabels];
        }
    } failure:nil];
    
    HLReportRequest *reportLabelRequest = [[HLReportRequest alloc] initWithReason];
    [reportLabelRequest startWithCompletionBlockWithSuccess:^(__kindof HLReportRequest * _Nonnull request) {
        if (request.isSuccess){
            self.reportList = [request reasonList];
            [self updateLabels];
        }
    } failure:nil];
    
    
    [_otherAvatarView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
    _otherNicknameLabel.text = [user shortName];
    
    [_meAvatarView sd_setImageWithURL:[NSURL URLWithString:HLRunTime.shareInstance.mine.avatar] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
}

- (void)evaluateAction:(QMUIButton *)btn
{
    self.generlButton.selected = NO;
    self.likeButton.selected = NO;
    
    btn.selected = YES;
    [self updateLabels];
    [self.scrollView setContentOffset:CGPointZero animated:NO];
}

- (void)submitAction:(QMUIButton *)btn
{
    btn.enabled = NO;
    NSString *hotEvaluate = @"0";
    NSString *label = @"";
    if (self.likeButton.isSelected){
        hotEvaluate = @"1";
        if (self.currentReason == nil){
            btn.enabled = YES;
            [QMUITips showWithText:@"请选择标签" inView:self];
            return;
        }
        label = self.currentReason.labelId;
        
        NSDictionary *params = @{
            @"femaleUserId":_user.userId,
            @"hotEvaluate":hotEvaluate,
            @"likeLabel":label,
            @"maleClient":ClientID,
            @"userId":HLRunTime.shareInstance.mine.userId,
        };
        HLRtcEvaluateRequest *evaluateLabelRequest = [[HLRtcEvaluateRequest alloc] initWithType:HLRtcEvaluateTypeSave params:params];
        [evaluateLabelRequest startWithCompletionBlockWithSuccess:^(__kindof HLRtcEvaluateRequest * _Nonnull request) {
            btn.enabled = YES;
            if (request.isSuccess){
                if (self.closeCallback){
                    self.closeCallback(YES);
                }
            } else {
                [QMUITips showWithText:request.message inView:self];
            }
        } failure:^(__kindof HLRtcEvaluateRequest * _Nonnull request) {
            btn.enabled = YES;
            [QMUITips showWithText:request.message inView:self];
        }];
        
    } else {
        
        if (self.currentReport == nil){
            [QMUITips showWithText:@"请选择标签" inView:self];
            btn.enabled = YES;
            return;
        }
        NSDictionary *params = @{
            @"reportClassCode": self.currentReport.reportClassCode,
            @"reportClassName": self.currentReport.reportClassName,
            @"reportContent": @"一般",
            @"reportType": @(4),
            @"reportUserId":_user.userId,
            @"userReportOpus":@[],
        };
        HLReportRequest *reportRequest = [[HLReportRequest alloc] initWithParams:params];
        [reportRequest startWithCompletionBlockWithSuccess:^(__kindof HLReportRequest * _Nonnull request) {
            btn.enabled = YES;
            if (request.isReport){
                if (self.closeCallback){
                    self.closeCallback(YES);
                }
            } else {
                [QMUITips showWithText:request.message inView:self];
            }
        } failure:^(__kindof HLReportRequest * _Nonnull request) {
            btn.enabled = YES;
            [QMUITips showWithText:request.message inView:self];
        }];
    }
    
}

+ (void)showWithUser:(HLUserInfoModel *)user time:(NSString *)time
{
    
    HLRtcEvaluateRequest *firstRequest = [[HLRtcEvaluateRequest alloc] initWithType:HLRtcEvaluateTypeFirst params:@{@"femaleUserId":user.userId}];
    
    [firstRequest startWithCompletionBlockWithSuccess:^(__kindof HLRtcEvaluateRequest * _Nonnull request) {
        if (request.isSuccess && [[request isFirst] boolValue]){
            CGFloat height = 516;

            HLRtcResultView *contentView = [[HLRtcResultView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
            contentView.user = user;
            contentView.rtcTimeLabel.text = time;
            QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];
            modalViewController.contentViewMargins = UIEdgeInsetsMake(SCREEN_HEIGHT-height, 0, 0, 0);
            modalViewController.contentView = contentView;
            modalViewController.dimmingView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.3];
            [modalViewController showWithAnimated:YES completion:nil];
            
            [contentView.closeButton setQmui_tapBlock:^(__kindof UIControl *sender) {
                [modalViewController hideWithAnimated:NO completion:nil];
            }];
            
            contentView.closeCallback = ^(BOOL isClose) {
                [modalViewController hideWithAnimated:NO completion:nil];
            };
        }
    } failure:nil];
}

@end
